﻿using Braincase.GanttChart;
using GALibrary.Entity;
using GALibrary.FormTemplate;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Comp307_GroupAssignment
{
    public partial class ChartForm : BaseForm
    {
        ProjectManager _mProject;
        Chart _mChart = new Chart();
        Dictionary<int, Color> taskColor;
        List<TaskSchedulerGA> recordTask;

        public ChartForm()
        {
            InitializeComponent();
           
        }
        public ChartForm(Dictionary<int, Color> _taskColor, List<TaskSchedulerGA> _recordTask) : this()
        {
            taskColor = _taskColor;
            recordTask = _recordTask;
            initChart();
        }

        public void initChart()
        {
            _mProject = new ProjectManager();

            //_mProject.Add(new Braincase.GanttChart.Task() { Name = "New Task" });
            var a = new Braincase.GanttChart.Task() { Name = "New Task2" };
            var b = new Braincase.GanttChart.Task() { Name = "New Task3" };
            var c = new Braincase.GanttChart.Task() { Name = "New Task4" };
            //_mProject.Add(a);
            //_mProject.Add(b);
            //_mProject.Add(c);
            _mProject.Split(a, b, c, 3);
            //_mProject.Add(a);
            _mProject.SetStart(a, 1);
            _mProject.SetStart(b, 1);
            _mProject.SetStart(c, 15);
            _mProject.SetDuration(a, 10);
            //_mProject.SetDuration(b, 1);
            //_mProject.SetDuration(c, 2);
            _mProject.TimeScale = TimeScale.Day;
            _mProject.Start.AddMilliseconds(1000);
            // _mProject.Add(a);
            _mProject.SetStart(a, 8);
            _mProject.SetDuration(a, 2);
            List<ColoredTask> ct = new List<ColoredTask>();
            foreach (var item in recordTask)
            {
                var temp = new ColoredTask() { Name = item.TaskId.ToString() , Color = taskColor[item.TaskId] };
                _mProject.Add(temp);
                _mProject.SetStart(temp, item.StartingTime);
                _mProject.SetDuration(temp, item.Duration);
            }
            
            

            //_mProject.Add(new ColoredTask() { Name = "Purple Task", Color = this.getRandomColor() });
            //_mProject.Add(new ColoredTask() { Name = "Gold Task", Color = Color.Gold });
            _mChart.Init(_mProject);
            // Custom behavior on paint task
            _mChart.PaintTask += (s, e) =>
            {
                ColoredTask ctask = e.Task as ColoredTask;
                if (ctask != null)
                {
                    var format = new TaskFormat();
                    format = e.Format;
                    format.BackFill = new SolidBrush(ctask.Color);
                    e.Format = format;
                }
            };

            // Grab custom data for tasks
            _mChart.TaskSelected += (s, e) =>
            {
                ColoredTask ctask = e.Task as ColoredTask;
                if (ctask != null)
                {
                    MessageBox.Show("Selected " + ctask.Color.ToString());
                }
            };
            this.Controls.Add(_mChart);
        }

        public class ColoredTask : Braincase.GanttChart.Task
        {
            public ColoredTask() : base() { }
            public Color Color { get; set; }
        }
    }
}
